{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ac993f9f",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/learn/analytics-and-ml/model-training/training-with-wandb/03-query.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/learn/analytics-and-ml/model-training/training-with-wandb/03-query.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f3cf55f",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -qq wandb datasets pinecone-client sentence-transformers transformers"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "afcfd398",
   "metadata": {},
   "source": [
    "## Encoder Training\n",
    "\n",
    "This is part *four* of a four-part notebook series on fine-tuning encoder models with Weights & Biases for use with Pinecone. Find the [full set of notebooks on Github here](https://github.com/pinecone-io/examples/blob/master/analytics-and-ml/model-training/training-with-wandb).\n",
    "\n",
    "We start by initializing our connection to Pinecone. You should already have an [API key from here](https://app.pinecone.io) if following the previous notebooks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "011cbb74-0f0a-46e2-b89a-69bb34cc2f30",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pinecone import Pinecone\n",
    "\n",
    "pinecone.init(\n",
    "    api_key='YOUR_API_KEY',  # app.pinecone.io\n",
    "    environment='YOUR_ENV'  # find next to api key in console\n",
    ")\n",
    "\n",
    "index_id = 'arxiv-search'\n",
    "\n",
    "# connect to index\n",
    "index = pinecone.Index(index_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c57c7158-4b44-495e-912c-5dd3c14ad8f8",
   "metadata": {},
   "source": [
    "We load the model from W&B as before..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2c443779-13b2-40a0-89da-1e89ada0ad8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.7/site-packages/IPython/html.py:14: ShimWarning: The `IPython.html` package has been deprecated since IPython 4.0. You should import from `notebook` instead. `IPython.html.widgets` has moved to `ipywidgets`.\n",
      "  \"`IPython.html.widgets` has moved to `ipywidgets`.\", ShimWarning)\n",
      "\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33mjamesbriggs\u001b[0m. Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "Tracking run with wandb version 0.13.5"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Run data is saved locally in <code>/home/jupyter/wandb/wandb/run-20221110_083930-2d602b1c</code>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Syncing run <strong><a href=\"https://wandb.ai/jamesbriggs/arxiv-searching/runs/2d602b1c\" target=\"_blank\">atomic-oath-36</a></strong> to <a href=\"https://wandb.ai/jamesbriggs/arxiv-searching\" target=\"_blank\">Weights & Biases</a> (<a href=\"https://wandb.me/run\" target=\"_blank\">docs</a>)<br/>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact minilm-arxiv:latest, 128.23MB. 6 files... \n",
      "\u001b[34m\u001b[1mwandb\u001b[0m:   6 of 6 files downloaded.  \n",
      "Done. 0:0:0.1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'./artifacts/minilm-arxiv:v1'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import wandb\n",
    "\n",
    "run = wandb.init(project=\"arxiv-searching\")\n",
    "\n",
    "artifact = run.use_artifact(\n",
    "    'jamesbriggs/arxiv-searching/minilm-arxiv:latest', type='model'\n",
    ")\n",
    "artifact_dir = artifact.download()\n",
    "artifact_dir"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "467a9954-200f-49c4-8539-a0269e7c14d1",
   "metadata": {},
   "source": [
    "Initialize it as a sentence transformer..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7af9d2be-1144-4ef4-88f3-43b57371dda0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SentenceTransformer(\n",
       "  (0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: BertModel \n",
       "  (1): Pooling({'word_embedding_dimension': 384, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})\n",
       ")"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sentence_transformers import models, SentenceTransformer\n",
    "import torch\n",
    "\n",
    "minilm = models.Transformer(artifact_dir)\n",
    "pooling = models.Pooling(\n",
    "    minilm.get_word_embedding_dimension(),\n",
    "    pooling_mode_mean_tokens=True\n",
    ")\n",
    "\n",
    "model = SentenceTransformer(\n",
    "    modules=[minilm, pooling],\n",
    "    device='cuda:1' if torch.cuda.is_available() else 'cpu'\n",
    ")\n",
    "model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "875eba46",
   "metadata": {},
   "source": [
    "Let's create an initial query and matching context."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "667760a0-4e86-43c3-b5ee-00e07104e91c",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"Ginzburg Landau theory for d-wave pairing and fourfold symmetric vortex\\n  core structure\"\n",
    "\n",
    "xq = model.encode(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "58dcd6e2-d699-42a5-b0a0-6f5946a27f44",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = \"  The Ginzburg Landau theory for d_{x^2-y^2}-wave superconductors is\\nconstructed, by starting from the Gor'kov equation with including correction\\nterms up to the next order of ln(T_c/T). Some of the non-local correction terms\\nare found to break the cylindrical symmetry and lead to the fourfold symmetric\\ncore structure, reflecting the internal degree of freedom in the pair\\npotential. Using this extended Ginzburg Landau theory, we investigate the\\nfourfold symmetric structure of the pair potential, current and magnetic field\\naround an isolated single vortex, and clarify concretely how the vortex core\\nstructure deviates from the cylindrical symmetry in the d_{x^2-y^2}-wave\\nsuperconductors.\\n\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fc26902e-a4c8-4ebc-a39e-4dbd653897b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "cc = model.encode(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fa96ac5-0a23-4eb8-b8c9-56d3656caca5",
   "metadata": {},
   "source": [
    "Let's do a quick sanity check:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ff071de5-5b47-4525-b0b6-c1e904021631",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sentence_transformers.util import cos_sim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5229398f-c638-4bfd-9317-84c3191da274",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.6709]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cos_sim(cc, xq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d6182e2c-edd1-419f-ad9d-d0eb278e3154",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = \"something completely random that has nothing to do with the query\"\n",
    "rr = model.encode(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "dfac7760-c2c9-46c7-bf31-e84f8dfee782",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.0784]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cos_sim(rr, xq)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f63bd5d5-bdd1-4345-8e7b-fa09bae30f06",
   "metadata": {},
   "source": [
    "Looks good the cosine similarity between the actual pair is high, whereas between something complete random it is not. Now let's begin performing actual queries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d674d177-4121-4614-8c64-2d4d04575de6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'matches': [{'id': '1304.4032',\n",
       "              'metadata': {'abstract': '  A procedure to derive the '\n",
       "                                       'Ginzburg-Landau (GL) theory from the '\n",
       "                                       'multiband BCS\\n'\n",
       "                                       'Hamiltonian is developed in a general '\n",
       "                                       'case with an arbitrary number of '\n",
       "                                       'bands\\n'\n",
       "                                       'and arbitrary interaction matrix. It '\n",
       "                                       \"combines the standard Gor'kov \"\n",
       "                                       'truncation\\n'\n",
       "                                       'and a subsequent reconstruction in '\n",
       "                                       'order to match accuracies of the '\n",
       "                                       'obtained\\n'\n",
       "                                       'terms. This reconstruction recovers '\n",
       "                                       'the phenomenological GL theory as '\n",
       "                                       'obtained\\n'\n",
       "                                       'from the Landau model of phase '\n",
       "                                       'transitions but offers explicit '\n",
       "                                       'microscopic\\n'\n",
       "                                       'expressions for the relevant '\n",
       "                                       'parameters. Detailed calculations are '\n",
       "                                       'presented\\n'\n",
       "                                       'for a three-band system treated as a '\n",
       "                                       'prototype multiband superconductor. It '\n",
       "                                       'is\\n'\n",
       "                                       'demonstrated that the symmetry in the '\n",
       "                                       'coupling matrix may lead to the '\n",
       "                                       'chiral\\n'\n",
       "                                       'ground state with the phase '\n",
       "                                       'frustration, typical for systems with '\n",
       "                                       'broken\\n'\n",
       "                                       'time-reversal symmetry.\\n'},\n",
       "              'score': 0.653086662,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '0710.3482',\n",
       "              'metadata': {'abstract': '  We succeed to build up a '\n",
       "                                       'straightforward theoretical model for '\n",
       "                                       'spin-triplet\\n'\n",
       "                                       'p-wave superconductors by introducing '\n",
       "                                       'in Ginzburg-Landau theory a second '\n",
       "                                       'order\\n'\n",
       "                                       'parameter and a suitable interaction '\n",
       "                                       'between the two mean fields.\\n'},\n",
       "              'score': 0.591999769,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '1110.1593',\n",
       "              'metadata': {'abstract': '  We report a microscopic derivation '\n",
       "                                       'of two-component Ginzburg-Landau (GL)\\n'\n",
       "                                       'field theory and the conditions of its '\n",
       "                                       'validity in two-band superconductors. '\n",
       "                                       'We\\n'\n",
       "                                       'also investigate the conditions when '\n",
       "                                       'microscopically derived or\\n'\n",
       "                                       'phenomenological GL models fail and '\n",
       "                                       'one should resort to a microscopic\\n'\n",
       "                                       'description.\\n'\n",
       "                                       '  We show that besides being directly '\n",
       "                                       'applicable at elevated temperatures, '\n",
       "                                       'a\\n'\n",
       "                                       'version of a minimal two-component GL '\n",
       "                                       'theory in certain cases also gives\\n'\n",
       "                                       'accurate description of certain '\n",
       "                                       'aspects of a two-band system even '\n",
       "                                       'substantially\\n'\n",
       "                                       'far from $T_c$. This shows that '\n",
       "                                       'two-component GL model can be used '\n",
       "                                       'for\\n'\n",
       "                                       'addressing a wide range of questions '\n",
       "                                       'in multiband systems, in particular '\n",
       "                                       'vortex\\n'\n",
       "                                       'physics and magnetic response. We also '\n",
       "                                       'argue that single Ginzburg-Landau\\n'\n",
       "                                       'parameter cannot in general '\n",
       "                                       'characterize magnetic response of '\n",
       "                                       'multiband\\n'\n",
       "                                       'systems.\\n'},\n",
       "              'score': 0.577286422,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '0707.1736',\n",
       "              'metadata': {'abstract': '  The one-dimensional Ginzburg-Landau '\n",
       "                                       '(GL) Equation is considered. We use '\n",
       "                                       'the\\n'\n",
       "                                       'recently developed extended '\n",
       "                                       'F-expansion method to obtain spiral '\n",
       "                                       'wave solution\\n'\n",
       "                                       'of one-dimensional GL Equation.\\n'},\n",
       "              'score': 0.568699062,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '1111.1314',\n",
       "              'metadata': {'abstract': '  We elaborate on boundary conditions '\n",
       "                                       'for Ginzburg-Landau (GL) theory in '\n",
       "                                       'the\\n'\n",
       "                                       'case of external currents. We '\n",
       "                                       'implement a self-consistent theory '\n",
       "                                       'within the\\n'\n",
       "                                       'finite element method (FEM) and '\n",
       "                                       'present numerical results for a '\n",
       "                                       'two-dimensional\\n'\n",
       "                                       'rectangular geometry. We emphasize '\n",
       "                                       'that our approach can in principle '\n",
       "                                       'also be\\n'\n",
       "                                       'used for general geometries in '\n",
       "                                       'three-dimensional superconductors.\\n'},\n",
       "              'score': 0.561762691,\n",
       "              'sparseValues': {},\n",
       "              'values': []}],\n",
       " 'namespace': ''}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xc = index.query(vector=xq.tolist(), top_k=5, include_metadata=True)\n",
    "xc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b997bd08-c5f9-4f31-b571-5c5f5954e8dc",
   "metadata": {},
   "source": [
    "We get a perfect result with the top item being the specific abstract for the given title. We can try with some more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1092133f-fe89-4234-95b6-c1852073e7de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'matches': [{'id': '2105.05674',\n",
       "              'metadata': {'abstract': '  Game developers benefit from '\n",
       "                                       'availability of custom game genres '\n",
       "                                       'when doing\\n'\n",
       "                                       'game market analysis. This information '\n",
       "                                       'can help them to spot opportunities '\n",
       "                                       'in\\n'\n",
       "                                       'market and make them more successful '\n",
       "                                       'in planning a new game. In this paper '\n",
       "                                       'we\\n'\n",
       "                                       'find good classifier for predicting '\n",
       "                                       'category of a game. Prediction is '\n",
       "                                       'based on\\n'\n",
       "                                       'description and title of a game. We '\n",
       "                                       'use 2443 iOS App Store games as data '\n",
       "                                       'set to\\n'\n",
       "                                       'generate a document-term matrix. To '\n",
       "                                       'reduce the curse of dimensionality we '\n",
       "                                       'use\\n'\n",
       "                                       'Latent Semantic Indexing, which, '\n",
       "                                       'reduces the term dimension to '\n",
       "                                       'approximately\\n'\n",
       "                                       '1/9. Support Vector Machine supervised '\n",
       "                                       'learning model is fit to '\n",
       "                                       'pre-processed\\n'\n",
       "                                       'data. Model parameters are optimized '\n",
       "                                       'using grid search and 20-fold cross\\n'\n",
       "                                       'validation. Best model yields to 77% '\n",
       "                                       'mean accuracy or roughly 70% accuracy '\n",
       "                                       'with\\n'\n",
       "                                       '95% confidence. Developed classifier '\n",
       "                                       'has been used in-house to assist '\n",
       "                                       'games\\n'\n",
       "                                       'market research.\\n'},\n",
       "              'score': 0.696358502,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '2101.00992',\n",
       "              'metadata': {'abstract': '  We develop methods to formally '\n",
       "                                       'describe and compare games, in order '\n",
       "                                       'to probe\\n'\n",
       "                                       'questions of game structure and '\n",
       "                                       'design, and as a stepping stone to '\n",
       "                                       'predicting\\n'\n",
       "                                       'player behavior from design patterns. '\n",
       "                                       'We define a grammar-like formalism to\\n'\n",
       "                                       'describe finite discrete games without '\n",
       "                                       'hidden information, allowing for\\n'\n",
       "                                       'randomness, and mixed sequential and '\n",
       "                                       'simultaneous play. We make minimal\\n'\n",
       "                                       'assumptions about the form or content '\n",
       "                                       'of game rules or user interface. The\\n'\n",
       "                                       'associated game trees resemble hybrid '\n",
       "                                       'extensive- and strategic-form games, '\n",
       "                                       'in\\n'\n",
       "                                       'the game theory sense. By transforming '\n",
       "                                       'and comparing game trees, we develop\\n'\n",
       "                                       'equivalence relations on the space of '\n",
       "                                       'game systems, which equate games that\\n'\n",
       "                                       'give players the same meaningful '\n",
       "                                       'agency. We bring these together to '\n",
       "                                       'suggest a\\n'\n",
       "                                       'method to measure distance between '\n",
       "                                       'games, insensitive to cosmetic '\n",
       "                                       'variations in\\n'\n",
       "                                       'the game logic descriptions.\\n'},\n",
       "              'score': 0.654641,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '1912.01876',\n",
       "              'metadata': {'abstract': '  Many problems can be viewed as '\n",
       "                                       'games, where one or more agents try to '\n",
       "                                       'ensure\\n'\n",
       "                                       'that certain objectives hold no matter '\n",
       "                                       'the behavior from the environment and\\n'\n",
       "                                       'other agents. In recent years, a '\n",
       "                                       'number of logical formalisms have '\n",
       "                                       'been\\n'\n",
       "                                       'proposed for specifying games among '\n",
       "                                       'which the Game Description Language '\n",
       "                                       '(GDL)\\n'\n",
       "                                       'was established as the official '\n",
       "                                       'language for General Game Playing. '\n",
       "                                       'Although\\n'\n",
       "                                       'numbers are recurring in games, the '\n",
       "                                       'description of games with numerical\\n'\n",
       "                                       'features in GDL requires the '\n",
       "                                       'enumeration from all possible numeric '\n",
       "                                       'values and\\n'\n",
       "                                       'the relation among them. Thereby, in '\n",
       "                                       'this paper, we introduce the Game\\n'\n",
       "                                       'Description Logic with Integers (GDLZ) '\n",
       "                                       'to describe games with numerical\\n'\n",
       "                                       'variables, numerical parameters, as '\n",
       "                                       'well as to perform numerical '\n",
       "                                       'comparisons.\\n'\n",
       "                                       'We compare our approach with GDL and '\n",
       "                                       'show that when describing the same '\n",
       "                                       'game,\\n'\n",
       "                                       'GDLZ is more compact.\\n'},\n",
       "              'score': 0.652700245,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '1711.10860',\n",
       "              'metadata': {'abstract': '  Game semantics is a rich and '\n",
       "                                       'successful class of denotational '\n",
       "                                       'models for\\n'\n",
       "                                       'programming languages. Most game '\n",
       "                                       'models feature a rather intuitive '\n",
       "                                       'setup, yet\\n'\n",
       "                                       'surprisingly difficult proofs of such '\n",
       "                                       'basic results as associativity of\\n'\n",
       "                                       'composition of strategies. We set out '\n",
       "                                       'to unify these models into a basic\\n'\n",
       "                                       'abstract framework for game semantics, '\n",
       "                                       'game settings. Our main contribution '\n",
       "                                       'is\\n'\n",
       "                                       'the generic construction, for any game '\n",
       "                                       'setting, of a category of games and\\n'\n",
       "                                       'strategies. Furthermore, we extend the '\n",
       "                                       'framework to deal with innocence, and\\n'\n",
       "                                       'prove that innocent strategies form a '\n",
       "                                       'subcategory. We finally show that our\\n'\n",
       "                                       'constructions cover many concrete '\n",
       "                                       'cases, mainly among the early models '\n",
       "                                       'and the\\n'\n",
       "                                       'very recent sheaf-based ones.\\n'},\n",
       "              'score': 0.640093,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '2207.11690',\n",
       "              'metadata': {'abstract': '  Machine sound classification has '\n",
       "                                       'been one of the fundamental tasks of '\n",
       "                                       'music\\n'\n",
       "                                       'technology. A major branch of sound '\n",
       "                                       'classification is the classification '\n",
       "                                       'of\\n'\n",
       "                                       'music genres. However, though covering '\n",
       "                                       'most genres of music, existing music\\n'\n",
       "                                       'genre datasets often do not contain '\n",
       "                                       'fine-grained labels that indicate the\\n'\n",
       "                                       'detailed sub-genres of music. In '\n",
       "                                       'consideration of the consistency of '\n",
       "                                       'genres of\\n'\n",
       "                                       'songs in a mixtape or in a DJ (live) '\n",
       "                                       'set, we have collected and annotated '\n",
       "                                       'a\\n'\n",
       "                                       'dataset of house music that provide 4 '\n",
       "                                       'sub-genre labels, namely future '\n",
       "                                       'house,\\n'\n",
       "                                       'bass house, progressive house and '\n",
       "                                       'melodic house. Experiments show that '\n",
       "                                       'our\\n'\n",
       "                                       'annotations well exhibit the '\n",
       "                                       'characteristics of different '\n",
       "                                       'categories. Also, we\\n'\n",
       "                                       'have built baseline models that '\n",
       "                                       'classify the sub-genre based on the\\n'\n",
       "                                       'mel-spectrograms of a track, achieving '\n",
       "                                       'strongly competitive results. '\n",
       "                                       'Besides,\\n'\n",
       "                                       'we have put forward a few application '\n",
       "                                       'scenarios of our dataset and baseline\\n'\n",
       "                                       'model, with a simulated sci-fi tunnel '\n",
       "                                       'as a short demo built and rendered in '\n",
       "                                       'a\\n'\n",
       "                                       '3D modeling software, with the colors '\n",
       "                                       'of the lights automated by the output '\n",
       "                                       'of\\n'\n",
       "                                       'our model.\\n'},\n",
       "              'score': 0.626893759,\n",
       "              'sparseValues': {},\n",
       "              'values': []}],\n",
       " 'namespace': ''}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"classifier for genre categorization of a game\"\n",
    "\n",
    "xq = model.encode(query).tolist()\n",
    "\n",
    "xc = index.query(vector=xq, top_k=5, include_metadata=True)\n",
    "xc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfccb607-11fa-4d5a-92d0-fd516c1efa1c",
   "metadata": {},
   "source": [
    "Another great result, let's try one more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b43dd25b-0e1d-46c6-b1e9-89183615abb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'matches': [{'id': '2206.05555',\n",
       "              'metadata': {'abstract': '  Multi-modal pre-training and '\n",
       "                                       'knowledge discovery are two important '\n",
       "                                       'research\\n'\n",
       "                                       'topics in multi-modal machine '\n",
       "                                       'learning. Nevertheless, none of '\n",
       "                                       'existing works\\n'\n",
       "                                       'make attempts to link knowledge '\n",
       "                                       'discovery with knowledge guided '\n",
       "                                       'multi-modal\\n'\n",
       "                                       'pre-training. In this paper, we '\n",
       "                                       'propose to unify them into a '\n",
       "                                       'continuous\\n'\n",
       "                                       'learning framework for mutual '\n",
       "                                       'improvement. Taking the open-domain '\n",
       "                                       'uni-modal\\n'\n",
       "                                       'datasets of images and texts as input, '\n",
       "                                       'we maintain a knowledge graph as the\\n'\n",
       "                                       'foundation to support these two tasks. '\n",
       "                                       'For knowledge discovery, a '\n",
       "                                       'pre-trained\\n'\n",
       "                                       'model is used to identify cross-modal '\n",
       "                                       'links on the graph. For model\\n'\n",
       "                                       'pre-training, the knowledge graph is '\n",
       "                                       'used as the external knowledge to '\n",
       "                                       'guide\\n'\n",
       "                                       'the model updating. These two steps '\n",
       "                                       'are iteratively performed in our '\n",
       "                                       'framework\\n'\n",
       "                                       'for continuous learning. The '\n",
       "                                       'experimental results on MS-COCO and '\n",
       "                                       'Flickr30K with\\n'\n",
       "                                       'respect to both knowledge discovery '\n",
       "                                       'and the pre-trained model validate '\n",
       "                                       'the\\n'\n",
       "                                       'effectiveness of our framework.\\n'},\n",
       "              'score': 0.777464151,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '2102.04883',\n",
       "              'metadata': {'abstract': '  This is an introductory '\n",
       "                                       'machine-learning course specifically '\n",
       "                                       'developed with\\n'\n",
       "                                       'STEM students in mind. Our goal is to '\n",
       "                                       'provide the interested reader with '\n",
       "                                       'the\\n'\n",
       "                                       'basics to employ machine learning in '\n",
       "                                       'their own projects and to familiarize\\n'\n",
       "                                       'themself with the terminology as a '\n",
       "                                       'foundation for further reading of the\\n'\n",
       "                                       'relevant literature. In these lecture '\n",
       "                                       'notes, we discuss supervised,\\n'\n",
       "                                       'unsupervised, and reinforcement '\n",
       "                                       'learning. The notes start with an '\n",
       "                                       'exposition of\\n'\n",
       "                                       'machine learning methods without '\n",
       "                                       'neural networks, such as principle '\n",
       "                                       'component\\n'\n",
       "                                       'analysis, t-SNE, clustering, as well '\n",
       "                                       'as linear regression and linear\\n'\n",
       "                                       'classifiers. We continue with an '\n",
       "                                       'introduction to both basic and '\n",
       "                                       'advanced\\n'\n",
       "                                       'neural-network structures such as '\n",
       "                                       'dense feed-forward and conventional '\n",
       "                                       'neural\\n'\n",
       "                                       'networks, recurrent neural networks, '\n",
       "                                       'restricted Boltzmann machines,\\n'\n",
       "                                       '(variational) autoencoders, generative '\n",
       "                                       'adversarial networks. Questions of\\n'\n",
       "                                       'interpretability are discussed for '\n",
       "                                       'latent-space representations and using '\n",
       "                                       'the\\n'\n",
       "                                       'examples of dreaming and adversarial '\n",
       "                                       'attacks. The final section is '\n",
       "                                       'dedicated to\\n'\n",
       "                                       'reinforcement learning, where we '\n",
       "                                       'introduce basic notions of value '\n",
       "                                       'functions and\\n'\n",
       "                                       'policy learning.\\n'},\n",
       "              'score': 0.728711367,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '2007.01126',\n",
       "              'metadata': {'abstract': '  Multi-task learning (MTL) optimizes '\n",
       "                                       'several learning tasks simultaneously '\n",
       "                                       'and\\n'\n",
       "                                       'leverages their shared information to '\n",
       "                                       'improve generalization and the '\n",
       "                                       'prediction\\n'\n",
       "                                       'of the model for each task. Auxiliary '\n",
       "                                       'tasks can be added to the main task '\n",
       "                                       'to\\n'\n",
       "                                       'ultimately boost the performance. In '\n",
       "                                       'this paper, we provide a brief review '\n",
       "                                       'on\\n'\n",
       "                                       'the recent deep multi-task learning '\n",
       "                                       '(dMTL) approaches followed by methods '\n",
       "                                       'on\\n'\n",
       "                                       'selecting useful auxiliary tasks that '\n",
       "                                       'can be used in dMTL to improve the\\n'\n",
       "                                       'performance of the model for the main '\n",
       "                                       'task.\\n'},\n",
       "              'score': 0.725683272,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '1703.05298',\n",
       "              'metadata': {'abstract': '  This report provides an introduction '\n",
       "                                       'to some Machine Learning tools within\\n'\n",
       "                                       'the most common development '\n",
       "                                       'environments. It mainly focuses on '\n",
       "                                       'practical\\n'\n",
       "                                       'problems, skipping any theoretical '\n",
       "                                       'introduction. It is oriented to both\\n'\n",
       "                                       'students trying to approach Machine '\n",
       "                                       'Learning and experts looking for new\\n'\n",
       "                                       'frameworks.\\n'},\n",
       "              'score': 0.724752188,\n",
       "              'sparseValues': {},\n",
       "              'values': []},\n",
       "             {'id': '2210.11024',\n",
       "              'metadata': {'abstract': '  Recently self supervised learning '\n",
       "                                       'has seen explosive growth and use in\\n'\n",
       "                                       'variety of machine learning tasks '\n",
       "                                       'because of its ability to avoid the '\n",
       "                                       'cost of\\n'\n",
       "                                       'annotating large-scale datasets.\\n'\n",
       "                                       '  This paper gives an overview for '\n",
       "                                       'best self supervised learning '\n",
       "                                       'approaches for\\n'\n",
       "                                       'multimodal learning. The presented '\n",
       "                                       'approaches have been aggregated by '\n",
       "                                       'extensive\\n'\n",
       "                                       'study of the literature and tackle the '\n",
       "                                       'application of self supervised '\n",
       "                                       'learning\\n'\n",
       "                                       'in different ways. The approaches '\n",
       "                                       'discussed are cross modal generation, '\n",
       "                                       'cross\\n'\n",
       "                                       'modal pretraining, cyclic translation, '\n",
       "                                       'and generating unimodal labels in '\n",
       "                                       'self\\n'\n",
       "                                       'supervised fashion.\\n'},\n",
       "              'score': 0.722871125,\n",
       "              'sparseValues': {},\n",
       "              'values': []}],\n",
       " 'namespace': ''}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"the best pre-training approaches for multi-modal machine learning\"\n",
    "\n",
    "xq = model.encode(query).tolist()\n",
    "\n",
    "xc = index.query(vector=xq, top_k=5, include_metadata=True)\n",
    "xc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f1c9ebd-8c6e-48b4-85d5-cd0f1695ed8b",
   "metadata": {},
   "source": [
    "In this case it seems the best result is in position *5* (still not bad out of 2M+ abstracts) and the top result seems relevant but might not quite answer our query."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "67dfc5ea",
   "metadata": {},
   "source": [
    "Once we're finished, we delete the index to save resources."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "144fd999",
   "metadata": {},
   "outputs": [],
   "source": [
    "pinecone.delete_index(index_id)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "14fa79c2",
   "metadata": {},
   "source": [
    "---"
   ]
  }
 ],
 "metadata": {
  "environment": {
   "kernel": "python3",
   "name": "common-cu110.m95",
   "type": "gcloud",
   "uri": "gcr.io/deeplearning-platform-release/base-cu110:m95"
  },
  "kernelspec": {
   "display_name": "Python 3.10.7 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "vscode": {
   "interpreter": {
    "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
